package reggie.ruiji.controller;


import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import reggie.ruiji.Dto.SetmealDto;
import reggie.ruiji.common.R;
import reggie.ruiji.pojo.PageBean;
import reggie.ruiji.pojo.Setmeal;
import reggie.ruiji.service.SetmealService;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto, HttpServletRequest request){
        setmealService.saveWithDish(setmealDto,request);
        return R.success("新增套餐成功");
    }

    @GetMapping("/page")
    public R<PageBean> getEmps(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer pageSize, String name)
    {
        PageBean S=setmealService.pagee(page,pageSize,name);

        return R.success(S);
    }

    @PostMapping("/status/{status}")
    public R<String> updateDishStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {

        setmealService.updateStatus(status, ids);

        if(status==1)
        {
            return R.success("启售成功");
        }
        else
        {
            return R.success("停售成功");
        }

    }

//表示删除setmealCache分类下所有的缓存数据
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public  R<String> delete(@RequestParam List<Long> ids)
    {
        setmealService.delete(ids);
        return R.success("删除成功");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id)
    {
        SetmealDto setmealDto=setmealService.getById(id);
        return R.success(setmealDto);
    }

    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status",unless = "#result.data==null")
    @GetMapping("/list")
    public R<List<Setmeal>> list( Setmeal setmeal)
    {
        List<Setmeal> list=setmealService.list(setmeal);
        return R.success(list);
    }


}
